﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.CMS;
using PowerCreator.LMS.CMS.Entity;
public partial class sysadmin_NewManager_AddNewTemplet : PowerCreator.LMS.Web.Base.BasePage
{
    NewTemplet obj = new NewTemplet();
    public int ID = 0;
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            if (Request.QueryString["ID"] != null)
            {
                ID = int.Parse(Request.QueryString["ID"].ToString());
                if (ID != 0)
                {
                    ViewState["ID"] = ID.ToString();
                    BinderForm(ID);
                }
                
            }
            if (Request.QueryString["Type"] != null)
            {
                if (Request.QueryString["Type"].ToString() == "upload")
                {
                    string path = Request.QueryString["fullpath"].ToString();
                    txt_Url.Text = path;
                }
            }
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 3)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }


    }
    protected override void InitPage()
    {
        base.InitPage();
    }
 
    private void BinderForm(int id)
    {
        VOTNewTemplet vo = new VOTNewTemplet();
        vo = obj.Load(id);

        txt_Url.Text = vo.Url;
        txt_Url.Enabled = false;
        tr_url.Visible = false;
        txt_Name.Text = vo.Name;
    }
    private bool CheckPageData()
    {
        string search = string.Format("Name = '{0}'", txt_Name.Text.Trim());
        if (ViewState["ID"] != null)
        {
            search += "and ID<>" + ViewState["ID"].ToString();
        }
        if (obj.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />此模板名称已存在，请更换！";
            return false;
        }
        
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTNewTemplet vo = new VOTNewTemplet();
        if (ViewState["ID"] != null)
        {
            vo = obj.Load(int.Parse(ViewState["ID"].ToString()));
        }
        vo.Url = txt_Url.Text.Trim();
        vo.Name = txt_Name.Text.Trim();
        PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
        vo.Creator = cinfo.UserID;
        vo.CreateTime = DateTime.Now;

        if (ViewState["ID"] == null)
        {
            if (obj.AddNewTemplet(vo))
            {
                string url = "NewTempletManage.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            if (obj.Update(vo))
            {
                string url = "NewTempletManage.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "NewTempletManage.aspx";
        Script.RedirectURL(url);
    }
}
